package com.polaris.live.ui.backpack.fragment

import android.os.Bundle
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.polaris.live.interface_bag.ShoppingMallCallBack
import com.polaris.live.R
import com.polaris.live.adapter.backpack.CommodityAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.databinding.FragmentCommodityBinding
import com.polaris.live.translation.R.string
import com.polaris.live.viewmodel.backpack.CommodityModel

/**
 * 商品
 * @author Created by 天晴 on 2024/7/17 13:52
 * @since 1.0.0
 **/
class CommodityFragment : BaseVbFragment<CommodityModel, FragmentCommodityBinding>() {

    private val mType: Long by lazy {
        arguments?.getLong("type") ?: 0L
    }
    private var mShoppingMallCallBack: ShoppingMallCallBack? = null

    private lateinit var mAdapter: CommodityAdapter

    private var mHidden: Boolean = true

    private var mFirstVisiblePosition = 0
    private var mLastVisiblePosition = 0

    override fun initView(savedInstanceState: Bundle?) {
        mAdapter = CommodityAdapter()
        mAdapter.addFooterView(View(context).apply {
            layoutParams = MarginLayoutParams(20.autoDp, 200.autoDp)
        })
        mBinding.recyclerView.setEnableRefresh(false)
        mBinding.recyclerView.setEmptyTip(R.drawable.family_empty, getString(string.no_hava_data))
        mBinding.recyclerView.apply {
            recycler.layoutManager =
                if (mType == CommonConst.DressUp.USER_PROFILE || mType == CommonConst.DressUp.LIVE_EFFECT) {
                    LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
                } else {
                    GridLayoutManager(requireContext(), 2)
                }
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@CommodityFragment,
                mViewModel.shopListLiveData
            ) {
                if (!mHidden) {
                    if (this == 0 && mAdapter.data.isNotEmpty() && mAdapter.getLastSelect() == -1) {
                        val orNull = mAdapter.data.getOrNull(0) ?: return@observe
                        mAdapter.setSelect(0)
                        mShoppingMallCallBack?.onSelectCallback(orNull)
                    }
                }
            }
            recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    recycler.post {
                        checkVisibleItems()
                    }

                }
            })
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            mAdapter.setSelect(position)
            mAdapter.getItemOrNull(position)?.let {
                mShoppingMallCallBack?.onSelectCallback(it)
            }
        }
        mViewModel.setObjectType(mType.toInt())
    }

    override fun initData() {
        super.initData()
        mViewModel.onRefresh()
    }

    private fun checkVisibleItems() {
        val layoutManager = mBinding.recyclerView.recycler.layoutManager as LinearLayoutManager
        val firstVisiblePosition = layoutManager.findFirstVisibleItemPosition()
        val lastVisiblePosition = layoutManager.findLastVisibleItemPosition()
        if (mFirstVisiblePosition != firstVisiblePosition || lastVisiblePosition != mLastVisiblePosition) {
            if (firstVisiblePosition > mFirstVisiblePosition) {
                for (i in mFirstVisiblePosition until firstVisiblePosition) {
                    mAdapter.notifyItemChanged(i, "hide")
                }
            }
            if (firstVisiblePosition < mFirstVisiblePosition) {
                for (i in firstVisiblePosition until mFirstVisiblePosition) {
                    mAdapter.notifyItemChanged(i, "show")
                }
            }
            if (lastVisiblePosition > mLastVisiblePosition) {
                for (i in mLastVisiblePosition + 1..lastVisiblePosition) {
                    mAdapter.notifyItemChanged(i, "show")
                }
            }
            if (lastVisiblePosition < mLastVisiblePosition) {
                for (i in lastVisiblePosition + 1..mLastVisiblePosition) {
                    mAdapter.notifyItemChanged(i, "hide")
                }
            }
            mFirstVisiblePosition = firstVisiblePosition
            mLastVisiblePosition = lastVisiblePosition

        }
    }

    fun setShoppingMallCallBack(l: ShoppingMallCallBack) {
        mShoppingMallCallBack = l
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        mHidden = hidden
        if (!hidden) {
//            show()
        }
    }

    fun show() {
        if (isCreated() && !mHidden) {
            mBinding.root.post {
                val index = mAdapter.getLastSelect()
                if (index == -1) {
                    val orNull = mAdapter.data.getOrNull(0) ?: return@post
                    mAdapter.setSelect(0)
                    mShoppingMallCallBack?.onSelectCallback(orNull)
                } else {
                    val orNull = mAdapter.data.getOrNull(index) ?: return@post
                    mShoppingMallCallBack?.onSelectCallback(orNull)
                }

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mShoppingMallCallBack = null
    }

}